<?php
/**
 * Created by PhpStorm.
 * User: czw
 * Date: 2018/7/11
 * Time: 14:59
 */
namespace app\common\model;

use app\lib\exception\CreateException;
use app\lib\exception\DeleteException;
use app\lib\exception\NotFoundException;
use app\lib\exception\UpdateException;

class Article extends BaseModel
{
    protected $hidden = ['delete_time'];

    public static function createOne($data){
        $res = self::create($data);
        if(!$res) throw new CreateException();
        return $res;
    }

    /**
     * 获取文章阅览
     * @param array $where
     * @return false|\PDOStatement|string|\think\Collection
     */
    public static function getAll($where = []){
        $res = self::where($where)->select();
        return $res;
    }
    public static function getById($id){
        $res = self::get($id);
        if(!$res) throw new NotFoundException();
    }
    public static function deleteOne($id){
        $article = self::get($id);
        if(!$article) throw new NotFoundException();
        $res = self::destroy($id);
        if(!$res) throw new DeleteException();
        return $res;
    }
    public static function updateOne($id,$data){
        $res = self::where('id='.$id)->update($data);
        if(!$res) throw new UpdateException();
    }

}